﻿using Spanner;

namespace SpannerWebSite
{
    internal partial class LibraryModelDemo : UI
    {
        // A simple global counter.
        public class GlobalCounterModel : LibraryModel {
            // The API for the Counter global model.
            public Var<int> N;
            public Var<Fn<Act>> Inc;
            // The constructor provides the implementation.
            public GlobalCounterModel(): base("GlobalCounter", null)
            {
                N = Var<int>("N", 0);
                Inc = Procedure("Inc", Set(N, N + 1));
            }
        }

        internal static GlobalCounterModel GlobalCounter = new GlobalCounterModel();

        internal static void WriteWebPage()
        {
            var xs = Var<int[]>("xs", Array<int>());

            // Use the Counter global model in our initialisation action.
            var InitAct = Do(
                Call(GlobalCounter.Inc),
                Push(xs, GlobalCounter.N),
                Call(GlobalCounter.Inc),
                Push(xs, GlobalCounter.N),
                Call(GlobalCounter.Inc),
                Push(xs, GlobalCounter.N)
            );

            var Content = Div().KoForEach(xs, x => P().KoText(x));

            var Dependencies = new Model[] { GlobalCounter };

            const string path = Const.DemoWebSitePath;
            var html = StandardSpannerHtmlPage("Library Model Demo", Content);
            var webPage = WebPage("LibraryModelDemo", html, InitAct, Dependencies);
            var ctxt = new Context(path);
            ctxt.WriteWebPage(webPage);
        }
    }
}
